package com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Selection;
import android.text.TextWatcher;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hq.fxh.CustomerView.switchbutton.SwitchButton;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.AdBannerInfo;
import com.hq.fxh.No3_TuiGuang.MyMoBan.ClearableEidtText;
import com.hq.fxh.R;
import com.hq.fxh.Utils.GlideUtils;


/**
 * @author 大圣
 * @ClassName CardAdItem
 * @Description : 名片广告
 * @Created 2015/11/13 0013
 * @Email qwqe601660957@163.com
 */
public class CardAdItem extends LinearLayout  {
    private FrameLayout flCardPicAdd;
    private ImageButton ibClose;
    private TextView tvAdTypeName;
    /** */
    private ImageView ivAd;
    /**
     * 姓名
     */
    private ClearableEidtText etName;
    /**
     * 手机号
     */
    private ClearableEidtText etPhone;
    /**
     * 联系地址
     */
    private ClearableEidtText etAddress;
    /**
     * 一键拨号
     */
     private TextView tvDes;
     private SwitchButton sbSwitch;
    private TextView tvSwitchInfo;
    private AdBannerInfo adBannerInfo;
    private AdBannerInfo.AdvertDetailBean Advert_detail;

    public CardAdItem(Context context) {
        super(context);
        inflate(context, R.layout.item_card_ad, this);
        findView();
        //改变默认的单行模式
        etName.setSingleLine(true);
//水平滚动设置为False
        etName.setHorizontallyScrolling(true);
        etAddress.setSingleLine(true);
        etAddress.setHorizontallyScrolling(true);
        tvAdTypeName.setText("名片广告");
 //        merchantToolSysConfigPresenter.getConfig2();
//        SpannableStringBuilder style = StringUtils.formatStyle(getContext().getResources().getColor(R.color.blue),"名片广告可上传头像，设置电话和联系地址，支持一键拨号。{0}","查看详情");

    }

//    private SpannableString getClickableSpan(String des,final String url) {
//        OnClickListener l = new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Bundle bundle = new Bundle();
//                bundle.putString("title","名片广告");
//                bundle.putString("url", url);
//                IntentUtils.openActivity(getContext(), WebActivity.class, bundle);
////                Toast.makeText(getContext(), "Click Success", Toast.LENGTH_SHORT).show();
//            }
//        };
//        SpannableString spanableInfo = new SpannableString(des + "查看详情");
//        int start = spanableInfo.toString().indexOf("查看详情");
//        int end = spanableInfo.length();
//        spanableInfo.setSpan(new Clickable(l), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        spanableInfo.setSpan(new ForegroundColorSpan(getContext().getResources().getColor(R.color.blue)), start, end,
//                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//        return spanableInfo;
//    }



    class Clickable extends ClickableSpan implements OnClickListener {
        private final OnClickListener mListener;

        public Clickable(OnClickListener l) {
            mListener = l;
        }

        @Override
        public void onClick(View v) {
            mListener.onClick(v);
        }
    }

    public void findView() {
        flCardPicAdd = (FrameLayout) findViewById(R.id.flCardPicAdd);
        ibClose = (ImageButton) findViewById(R.id.ibClose);
        tvAdTypeName = (TextView) findViewById(R.id.tvAdTypeName);
        ivAd = (ImageView) findViewById(R.id.ivAd);
        etName = (ClearableEidtText) findViewById(R.id.etName);
        etPhone = (ClearableEidtText) findViewById(R.id.etPhone);
        etAddress = (ClearableEidtText) findViewById(R.id.etAddress);
         tvDes = (TextView) findViewById(R.id.tvDes);
        sbSwitch = (SwitchButton) findViewById(R.id.sbSwitch);
        tvSwitchInfo = (TextView) findViewById(R.id.tvSwitchInfo);
    }

    public void setAdInfo(final AdBannerInfo adInfo) {
        adBannerInfo = adInfo;
        Advert_detail = adBannerInfo.getAdvert_detail();
        if (Advert_detail != null) {
            etName.setText(Advert_detail.getName());
            etPhone.setText(Advert_detail.getMobile());
            etAddress.setText(Advert_detail.getAddress());
         //   ImageLoader.getInstance().load(getContext(), ivAd, Advert_detail.getPic());
            String pic = Advert_detail.getPic();
            if (pic == null || pic.equals("")) {

            } else {
                GlideUtils.loadImage(getContext(), pic,ivAd);

            }        } else if (Advert_detail == null) {
            Advert_detail = new AdBannerInfo.AdvertDetailBean();
            adInfo.setAdvert_detail(Advert_detail);
        }
//        final MerchantArticleCardModel merchantArticleCardModel = adInfo.getMerchantArticleCardModel();
//        if(merchantArticleCardModel != null){
//            Pic pic = merchantArticleCardModel.getCardPic();
//            if(pic != null){
//                if(pic.isNative()){
//                    Bitmap bitmap = ImageUtils.bytes2Bimap(Base64.decode(pic.getPicData(), Base64.DEFAULT));
//                    ivAd.setImageBitmap(bitmap);
//                }else{
////                    DisplayImageOptions options = ImageUtils.getDisplayImageOptions();
//                    IImageLoader.getInstance().load(getContext(),ivAd,pic.getPicUrl());
//                }
//
//            }

        etName.setFilters(new InputFilter[]{new EmojiFilter()});
        etPhone.setFilters(new InputFilter[]{new EmojiFilter()});
        etAddress.setFilters(new InputFilter[]{new EmojiFilter()});
        etName.addTextChangedListener(new TextWatcher() {
            private int maxLen = 24;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Editable editable = etName.getText();
                int len = editable.length();
                if (len > maxLen) {
                    ToastUtils.show(getContext(), "输入字符不能超过"+maxLen+"个");
                    int selEndIndex = Selection.getSelectionEnd(editable);
                    String str = editable.toString();
                    //截取新字符串
                    String newStr = str.substring(0, maxLen);
                    etName.setText(newStr);
                    editable = etName.getText();

                    //新字符串的长度
                    int newLen = editable.length();
                    //旧光标位置超过字符串长度
                    if (selEndIndex > newLen) {
                        selEndIndex = editable.length();
                    }
                    //设置新光标所在的位置
                    Selection.setSelection(editable, selEndIndex);

                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                Advert_detail.setName(s.toString());
                adInfo.setAdvert_detail(Advert_detail);
            }
        });
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Advert_detail.setMobile(s.toString());
                adInfo.setAdvert_detail(Advert_detail);
            }
        });
        etAddress.addTextChangedListener(new TextWatcher() {
            private int maxLen = 50;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Editable editable = etAddress.getText();
                int len = editable.length();
                if (len > maxLen) {
                    ToastUtils.show(getContext(), "输入字符不能超过"+maxLen+"个");
                    int selEndIndex = Selection.getSelectionEnd(editable);
                    String str = editable.toString();
                    //截取新字符串
                    String newStr = str.substring(0, maxLen);
                    etAddress.setText(newStr);
                    editable = etAddress.getText();

                    //新字符串的长度
                    int newLen = editable.length();
                    //旧光标位置超过字符串长度
                    if (selEndIndex > newLen) {
                        selEndIndex = editable.length();
                    }
                    //设置新光标所在的位置
                    Selection.setSelection(editable, selEndIndex);

                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                Advert_detail.setAddress(s.toString());
                adInfo.setAdvert_detail(Advert_detail);
            }
        });


        sbSwitch.setChecked(BooleanUtils.intToBool(adInfo.getIs_carousel()));
        setSbSwitchText(sbSwitch.isChecked(), 0);
        sbSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                adInfo.setIs_carousel(BooleanUtils.BoolToInt(isChecked));
//                    adInfo.setCheckShow(adInfo.getCheckShow() +1);
//                    setSbSwitchText(isChecked,adInfo.getCheckShow());
            }
        });


    }
//        LddConfig lddConfig = merchantToolSysConfigPresenter.getConfig();
    // FIXME: 2016/11/1 0001  要改回2
    //  if(lddConfig != null){
//    tvDes.setClickable(true);
//            tvDes.setText(getClickableSpan(lddConfig.getCardDisplayWords(),lddConfig.getCardLinkUrl()));
//            tvDes.setText(lddConfig.getCardDisplayWords());
//    tvDes.setMovementMethod(LinkMovementMethod.getInstance());
    // }


    public void setSbSwitchText(boolean isCheck,int  checkShow){
        if(isCheck){
            tvSwitchInfo.setText("是否展现");
            if(checkShow > 0 ){
                ToastUtils.show(getContext(), R.string.to_show);
            }

        }else{
            tvSwitchInfo.setText("是否展现");
            if(checkShow > 0){
                ToastUtils.show(getContext(), R.string.to_hide);
            }
        }
    }


    public void setOnClickListener(final OnClickListener listener){
        ibClose.setOnClickListener(listener);
        ivAd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(flCardPicAdd);
            }
        });
        flCardPicAdd.setOnClickListener(listener);
    }
    public void setImage(String url) {
       // ImageLoader.getInstance().load(getContext(), ivAd, url);
        GlideUtils.loadImage(getContext(),url,ivAd);

        if (adBannerInfo != null && adBannerInfo.getAdvert_detail() != null) {
            adBannerInfo.getAdvert_detail().setPic(url);
        } else {
            AdBannerInfo.AdvertDetailBean advertDetailBean = new AdBannerInfo.AdvertDetailBean();
            advertDetailBean.setPic(url);
            advertDetailBean.setName("");
            advertDetailBean.setMobile("");
            advertDetailBean.setAddress("");
            adBannerInfo.setAdvert_detail(advertDetailBean);
        }
    }
}
